package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Menu;

import cn.itsource.system.mapper.MenuMapper;
import cn.itsource.system.query.MenuQuery;

import cn.itsource.system.service.IMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional

    public void save(Menu menu) {
        if (menu.getId() == null) {
            menuMapper.insert(menu);
        } else {
            menuMapper.updateByPrimaryKeySelective(menu);
        }
    }

    @Override
    @Transactional

    public int deleteByPrimaryKey(Long id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Menu selectByPrimaryKey(Long id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Menu> pageInfo(MenuQuery menuQuery) {
        PageHelper.startPage(menuQuery.getCurrentPage(), menuQuery.getPageSize());
        List<Menu> menus = menuMapper.selectAllByQuery(menuQuery);
        return new PageInfo<Menu>(menus);
    }

    @Override
    @Transactional
    public void deleteByCollection(List<Menu> menus) {
        menuMapper.deleteByConllection(menus);
    }

    @Override
    public List<Menu> getTree() {
        //查询到所有的
        List<Menu> all = menuMapper.getAll();

        //创建一个空集合 该集合主要是把没有层次接口的文章类型 改为有层次结构，并且响应给前端
        List<Menu> treeMenus = new ArrayList<>();
        /*//需要把list转为map 正常写法
        HashMap< Long, Menu> map1 = new HashMap<>();
        for (Menu menu : all) {
            map1.put(menu.getId(),menu);
        }*/
        //使用鲁昂木 表达式把list转成map
        Map<Long, Menu> map = all.stream()
                .collect(Collectors.toMap(Menu::getId, a -> a));
        //循环所有的文章类型
        for (Menu menu : all) {
            List<Long> list = treeMenus.stream().map(Menu::getId).toList();
            //如果父级id为空 就证明是一级文章类型
            if (menu.getParentId() == null) {
                //添加一级
                treeMenus.add(menu);
            } else {
                //如果父级id不为空 那么就说明其有父级 为二级或者n级
                //拿到父级类型对象
                Menu parentMenu = map.get(menu.getParentId());
                if (parentMenu==null){
                    treeMenus.add(menu);
                }else {
                    //把儿子装进去
                    parentMenu.getChildren().add(menu);
                    if (!list.contains(menu.getParentId())){
                        treeMenus.add(parentMenu);
                    }
                }

            }
        }
     return  treeMenus;
    }
}



